﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Mvc;
using System.Text.RegularExpressions;

namespace THS.HtmlHelpers
{
    public static class UrlEncoder
    {
        public static string EncodeFriendlyUrl(this UrlHelper helper,
            string originalUrl)
        {
            if (originalUrl.Length > 50)
                originalUrl = originalUrl.Substring(0, 50);
            originalUrl.ToLower();
            //string cleanUrl = Regex.Replace(originalUrl, @"[^a-z0-9\s-]", "_"); // invalid chars
            string cleanUrl = originalUrl;
            cleanUrl = Regex.Replace(originalUrl, @"\s+", "_").Trim().ToLower(); // convert multiple spaces into one
           // cleanUrl = Regex.Replace(cleanUrl, "=", "/");
            //return new Regex("[^a-zA-Z]").Replace("s", "_");
            //return cleanUrl+=".bbh";
            
            StringBuilder urlBulder = new StringBuilder();

            foreach (var ch in cleanUrl)
            {
                
                switch (ch)
                {
                    case ' ':
                        urlBulder.Append('_');
                        break;
                    case '&':
                        urlBulder.Append("và");
                        break;
                    case '\'':
                        break;
                    case'_':
                        urlBulder.Append('_');
                        break;
                    case '.':
                        urlBulder.Append('.');
                        break;
                    case 'à':
                    case 'á':
                    case 'ả':
                    case 'ã':
                    case 'ạ':
                    case 'ă':
                    case 'ằ':
                    case 'ắ':
                    case 'ẳ':
                    case 'ẵ':
                    case 'ặ':
                    case 'â':
                    case 'ầ':
                    case 'ấ':
                    case 'ẩ':
                    case 'ẫ':
                    case 'ậ':
                        urlBulder.Append('a');
                        break;
                    case 'ê':
                    case 'ề':
                    case 'ế':
                    case 'ể':
                    case 'ễ':
                    case 'ệ':
                        urlBulder.Append('e');
                        break;
                    case'ì':
                    case'í':
                    case'ỉ':
                    case'ĩ':
                    case'ị':
                        urlBulder.Append('i');
                        break;
                    case 'ù':
                    case 'ú':
                    case 'ủ':
                    case 'ũ':
                    case 'ụ':
                    case'ư':
                    case'ừ':
                    case'ứ':
                    case'ử':
                    case'ữ':
                    case'ự':
                        urlBulder.Append('u');
                        break;
                    case 'ò':
                    case 'ó':
                    case 'ỏ':
                    case 'õ':
                    case 'ọ':
                    case'ô':
                    case'ồ':
                    case'ố':
                    case'ổ':
                    case'ỗ':
                    case'ộ':
                    case'ơ':
                    case'ờ':
                    case'ớ':
                    case'ở':
                    case'ỡ':
                    case'ợ':
                        urlBulder.Append('o');
                        break;
                    case'đ':
                        urlBulder.Append('d');
                        break;
                    case'ỷ':
                    case'ỵ':
                    case'ỹ':
                        urlBulder.Append('y');
                        break;
                    default:
                        if ((ch >= '0' && ch <= '9') ||
                            (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
                        {
                            urlBulder.Append(ch);
                        }
                        else
                        {
                            urlBulder.Append('_');
                        }
                        break;
                }
            }
            String url = urlBulder.ToString();
            String realUrl = string.Empty;
            if (url[url.Length - 1] == '_')
                realUrl = url.Remove(url.Length - 1, 1);
            else 
                realUrl = url;
            return realUrl;
        }


    }
}